$(function () {
    var clipboard = new ClipboardJS('.copybtn');
    clipboard.on('success', function(e) {
        $('#alert7 .invite-tip').html('你的组队连接已复制到剪切板，快去粘贴邀请吧！');
        $('#alert7,.zhezhao').show();
        setTimeout(function(){
            $('#alert7 .invite-tip').html('');
            $('#alert7,.zhezhao').hide();
        },3000);

        e.clearSelection();
    });
    var GrpId = getQueryString('GrpId') || false,
        inviteUserId = getQueryString('InviteUserId')|| false,
        inviteUserPhone = getQueryString('InviteUserPhone')|| false;
    //请求全部战队
    $.ajax({
        type: 'get',
        url: '/ac/grp/getAllMyGrp',
        dataType: 'json',
        success: function(res){
            if(res.errno === 200){
                showAllGrp(res.data);
            }
        }
    });
    //参数都存在的话为邀请
    if(GrpId && inviteUserId && inviteUserPhone){
        $('.zhezhao').show();
        $('#alert2').show().find('.inviteUserPhone').html(inviteUserPhone);

    }
    //查看我的战队
    $('#check-team').on('click',function () {
        $('.zhezhao').show();
        $('#alert1').show().find('.code-img').attr('src','/ac/grp/getCode?t='+(+new Date()));
    });
    //点击验证码刷新
    $(".code-img").on('click',function(){
       $(this).attr('src','/ac/grp/getCode?t='+(+new Date()));
    });
    //创建队伍
    $('#creatMyGrp').on('click',function(){
        $('#alert4,.zhezhao').show();
    });
    //登录
    $('#alert1 .btn1').on('click',function(){
        $('#alert1,.zhezhao').hide();
    });
    $('#alert1 .btn2').on('click',function(){
        var $phone = $('#alert1 [name="phone"]');
        var $code = $('#alert1 [name="code"]');
        var phoneVal = $phone.val();
        var codeVal = $code.val();
        if(phoneVal.length!==11){
            $phone.siblings('.tips').html('手机号码有误');
            return;
        }
        if(codeVal.length !== 4){
            $code.siblings('.tips').html('验证码有误！');
            return;
        }
        $.ajax({
            type: 'post',
            url: '/ac/grp/getMyGrp',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify({
                "phone": phoneVal,
                "verifyCode": codeVal
            }),
            success: function(res){
                if(res.errno === 200){
                    if(res.data.myGrp){
                        $('.zhezhao').hide();
                        $('#alert1').hide().find('.tips').html('');
                        $('#see-team-form')[0].reset();
                        showMyGrp(res.data,phoneVal.substr(0,3)+'****'+phoneVal.substr(7,4));
                    }else{
                        $code.siblings('.tips').html("您不属于任何队伍，请先创建一直队伍！");
                    }
                }else{
                    $code.siblings('.tips').html(res.errmsg);
                    $code.val('');
                    $(".code-img").attr('src','/ac/grp/getCode?t='+(+new Date()));
                }
            }
        })
    });
    //输入框编辑的时候要去掉提示
    $('form input').on('input',function(){
        var $group = $(this).parents('.group').eq(0);
        $group.find('.tips').html('');
    });
    //alert2为邀请提示
    $('#alert2 .btn1').on('click',function () {
        $('#alert2,.zhezhao').hide();
    });
    $('#alert2 .btn2').on('click',function () {
        $('#alert2').hide();
        $('#alert3').show();
    });
    //alert3为接受邀请
    $('#alert3 .btn1').on('click',function () {
        $('#alert3,.zhezhao').hide();
    });
    $('#alert3 .btn2').on('click',function () {
        var $phone = $('#alert3 [name="phone"]');
        var phoneVal = $phone.val();
        if(phoneVal.length!==11){
            $phone.siblings('.tips').html('手机号码有误');
            return;
        }
        $.ajax({
            type: 'post',
            url: '/ac/grp/inGrp',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify({
                phone: phoneVal,
                inviteUserId: inviteUserId,
                grpId: GrpId
            }),
            success: function(res){
                if(res.errno === 200){
                    $('#alert3').hide();
                    $('#alert5').show().find('.invite-tip span').html('加入战队成功！');
                    history.pushState(null,'惠金所激情世界杯',"index.html")
                }else{
                    $phone.siblings('.tips').html(res.errmsg);
                }
            }
        })
    });
    //alert4为创建战队
    $('#alert4 .btn1').on('click',function () {
        $('#alert4,.zhezhao').hide();
    });
    $('#alert4 .btn2').on('click',function () {
        var $phone = $('#alert4 [name="phone"]');
        var phoneVal = $phone.val();
        if(phoneVal.length!==11){
            $phone.siblings('.tips').html('手机号码有误');
            return;
        }
        $.ajax({
            type: 'post',
            url: '/ac/grp/createMyGrp',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify({
                phone: phoneVal
            }),
            success: function(res){
                if(res.errno === 200){
                    if(res.data.isNewInvest===true){
                        $('#alert4').hide();
                        $('#alert6').show();
                    }else{
                        $('#alert4').hide();
                        $('#alert5').show().find('.invite-tip span').html('创建战队成功！');
                    }
                }else{
                    $('#tips1').html(res.errmsg).show();
                    setTimeout(function(){
                        $('#tips1').hide().html('');
                    },3000);
                }
            }
        })
    });
    //alert5为接受邀请成功或创建战队成功的提示
    $('#alert5 .btn1').on('click',function () {
        $('#alert5,.zhezhao').show();
    });
    $('#alert5 .btn2').on('click',function () {
        $('#alert5').hide();
        $('#alert1').hide().find('.tips').html('');
        $('#see-team-form')[0].reset();
        $('#alert1').show().find('.code-img').attr('src','/ac/grp/getCode?t='+(+new Date()));
    });
    //提示框
    $('#alert6 .btn2').on('click',function () {
        $('#alert6').hide();
        $('.zhezhao').hide();
    });
    //展示我的信息
    function showMyGrp(data,phone){
        $('#section-one1').hide();
        $('#section-one2').show();
        if(data.myGrp){
            $('#section-one2 .ballNum').html(data.myGrp.ballNm);
            $('#section-one2 .totalInvest').html(data.myGrp.totalInvest);
        }
        if(data.myGrpMembers){
            var htmlstr = "\n";
            for(var i=0,len=data.myGrpMembers.length;i<len;i++){
                htmlstr += '<li><img class="avator" src="./images/Nationalflag'+data.myGrpMembers[i].picIndex+'.png" alt="">'+data.myGrpMembers[i].phone+'</li>\n';
            }
            $('#section-one2 .people-list').html(htmlstr);
        }
        $('#invitation-btn').attr('data-clipboard-text','我在参加阳光保险惠金所世界杯狂欢活动，速度来组队！最高可瓜分28200元现金大奖！组队链接：'+window.location.origin + window.location.pathname + '?GrpId='+data.myGrp.grpId+'&inviteUserId='+data.me.id+'&inviteUserPhone='+phone+'，点击即可参与！');
    }

    //展示全部战队
    function showAllGrp(data) {
        var htmlstr = "";
        if(data.length>0){
            if(data.length>10) data.length = 10;
            for(var i=0,len=data.length;i<len;i++){
                htmlstr += '<li>\
                            <i class="num n'+(i+1)+'">'+i+'</i>\
                            <img class="avator" src="./images/Nationalflag'+data[i].picIndex+'.png" alt="">\
                            <span class="name">'+data[i].grpLeaderPhone+'的战队</span>\
                            <span class="ball-num">'+data[i].ballNm+'个</span>\
                            <i class="icon icon-ball"></i>\
                        </li>';

            }
        }else{
            htmlstr='<li style="text-align:center;">暂无队伍</li>';
        }
        $('.section-three .top-list').html(htmlstr);
    }

    //获取参数
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        var q = window.location.pathname.substr(1).match(reg_rewrite);
        if(r != null){
            return unescape(r[2]);
        }else if(q != null){
            return unescape(q[2]);
        }else{
            return null;
        }
    }
});